package com.itaem.community.modules.patrol.location.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.patrol.location.entity.PatrolLocationEntity;
import com.itaem.community.modules.patrol.location.service.PatrolLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 巡逻任务点 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "巡逻地点")
@ResponseResult
@RestController
@RequestMapping("/patrolLocation")
public class PatrolLocationController {

    @Autowired
    private PatrolLocationService patrolLocationService;

    /**
     * @param page 当前页数是第page页
     * @param size 每页最多有size个数据
     * @return page对象
     */
//    @RequiresRoles(value = {"超级管理员"})
    @ApiOperation("分页查询信息")
    @GetMapping("/{page}/{size}")
    public Page list(@PathVariable Long page, @PathVariable Long size){
        return patrolLocationService.page(new Page<PatrolLocationEntity>(page, size));
    }

    /**
     * @param id 获取id为id的信息
     * @return 该打卡地点的所有信息
     */
//    @RequiresRoles(value = {"超级管理员"})
    @ApiOperation("获取单条信息")
    @GetMapping("/{id}")
    public PatrolLocationEntity info(@PathVariable Long id){
        return patrolLocationService.getById(id);
    }

    /**
     * @param patrolLocationEntity 前端传入打卡地点的数据
     * @return 打卡地点添加是否成功
     */
//    @RequiresRoles(value = {"超级管理员"})
    @ApiOperation("新增")
    @PostMapping
    public String save(@RequestBody PatrolLocationEntity patrolLocationEntity) {
        List<PatrolLocationEntity> list = patrolLocationService.list();
//        List<Double> LatList = new ArrayList<>();
//        List<Double> LonList = new ArrayList<>();
//        for (PatrolLocationEntity patrolLocationEntityList:list) {
//            LatList.add(patrolLocationEntityList.getLatitude());
//            LonList.add(patrolLocationEntityList.getLongitude());
//        }
//        for (int i = 0; i < LatList.size(); i++) {
//            if (patrolLocationEntity.getLatitude().equals(LatList.get(i))){
//                return "打卡地点重复，添加失败";
//            }
//        }
//        for (int i = 0; i < LatList.size(); i++) {
//            if (patrolLocationEntity.getLongitude().equals(LonList.get(i))){
//                return "打卡地点重复，添加失败";
//            }
//        }
        for (PatrolLocationEntity patrolLocationEntityList : list){
            if (patrolLocationEntityList.getLatitude().equals(patrolLocationEntity.getLatitude()) && patrolLocationEntityList.getLongitude().equals(patrolLocationEntity.getLongitude())) {
                return "打卡地点重复，添加失败";
            }
        }
        patrolLocationService.save(patrolLocationEntity);
        return  "打卡地点添加成功";
    }

    /**
     * @param patrolLocationEntity 前端传入更新打卡地点的数据
     * @return 更新打卡地点信息是否成功
     */
//    @RequiresRoles(value = {"超级管理员"},logical = Logical.OR)
    @ApiOperation("更新")
    @PatchMapping
    public Boolean update(@RequestBody PatrolLocationEntity patrolLocationEntity) {
        UpdateWrapper<PatrolLocationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",patrolLocationEntity.getId());
        return patrolLocationService.update(patrolLocationEntity,updateWrapper);
    }

    /**
     * @param id 需要删除的数据 (逻辑删除)
     * @return 删除是否成功
     */
//    @RequiresRoles(value = {"超级管理员"},logical = Logical.OR)
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public String remove(@PathVariable Long id) {
        patrolLocationService.removeById(id);
        return patrolLocationService.removeById(id) ? "删除成功" : "没有此纪录";
    }
}

